<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('项目名称')" prop="dealId">
            <el-link class="anywhere" type="primary" :underline="false" @click="editForm">
              {{ form.dealName }}
            </el-link>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('申请标题')" prop="actTitle">
            <el-input v-model="form.actTitle" :placeholder="$t('申请标题')" show-word-limit :maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('项目负责人')" prop="personCharge">
            <UserSelect
              v-model="form.personCharge"
              :placeholder="$t('项目负责人')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('项目类型')" prop="dealType">
            <dictSelect
              v-model="form.dealType"
              :clearable="true"
              dictionary="deal_type"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('预计投资时间')" prop="dateInvestmentEstimate">
            <DatePicker v-model="form.dateInvestmentEstimate" :placeholder="$t('预计投资时间')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('预计投资金额') + $t('金额单位')" prop="amountInvestmentEstimate">
            <Money v-model="form.amountInvestmentEstimate" unit="w" :placeholder="$t('预计投资金额')" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <dealBaseBondForm
          v-if="form.dealWfStorageBond !== undefined && form.dealWfStorageBond !== null"
          ref="dealBaseBondForm"
          :bond-form="form.dealWfStorageBond"
          :other-btn-hide="otherBtnHide"
        />
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('项目来源')" prop="dealSource">
            <el-input v-model="form.dealSource" :placeholder="$t('项目来源')" show-word-limit :maxlength="10" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('项目简介')" prop="dealIntroduction">
            <el-input v-model="form.dealIntroduction" type="textarea" :placeholder="$t('项目简介')" show-word-limit maxlength="2000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('项目主要优点')" prop="dealStrongPoint">
            <el-input v-model="form.dealStrongPoint" type="textarea" :placeholder="$t('项目主要优点')" show-word-limit maxlength="2000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('项目主要顾虑')" prop="dealScruple">
            <el-input v-model="form.dealScruple" type="textarea" :placeholder="$t('项目主要顾虑')" show-word-limit maxlength="2000" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 附件列表 -->
      <!--      <FileTable :list.sync="form.fileList" data-type="deal_wf_bond_base" :business-id="form.dealId" :btn-show="!otherBtnHide" />-->
    </el-form>
    <!-- 添加或修改项目基本信息对话框 -->
    <el-dialog :title="$t('项目基本信息')" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealBaseForm ref="dealBaseForm" />
    </el-dialog>
  </div>
</template>

<script>
import dealBaseBondForm from '@/views/deal/base/bond/dealBaseBondForm'
import dealBaseForm from '@/views/deal/base/dealBaseForm.vue'
import { saveDealWfStorage, getDealWfStorage, getByWfNodeId } from '@/api/deal/form/dealWfStorage'
export default {
  components: {
    dealBaseBondForm,
    dealBaseForm
  },
  filters: {
    ellipsis(value) {
      if (!value) return ''
      if (value.length > 15) {
        return value.slice(0, 15) + '...'
      }
      return value
    }
  },
  data() {
    return {
      // 表单参数
      form: {
        dealWfStorageBond: {
          primaryBondCategory: '',
          secondaryBondCategory: '',
          bondCode: undefined,
          bondName: undefined,
          financingSubject: undefined,
          obligor: undefined,
          countryRisk: undefined,
          subjectName: undefined,
          series: undefined,
          nameCreditorRight: undefined,
          fundManager: undefined,
          fundSize: undefined,
          termInvestment: undefined,
          industryLevelOne: undefined,
          industryLevelTwo: undefined,
          currency: undefined,
          dueDate: undefined,
          repaymentOrder: undefined,
          level: undefined,
          structuredType: undefined,
          bondType: undefined,
          natureEnterprise: undefined,
          externalRatingIssuer: undefined,
          nameGuarantor: undefined,
          ratingOutsideGuarantor: undefined,
          thirdPartyPrincipal: undefined,
          dealBaseBondLevelList: []
        }
      },
      // 遮罩层
      loading: false,
      // 是否隐藏保存按钮
      otherBtnHide: false,
      formOpen: false,
      // 表单校验
      rules: {
        actTitle: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
        dealType: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
        personCharge: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
      }
    }
  },
  created() {
    this.$bus.$emit('async_done')
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getDealWfStorage(data.id).then(response => {
          this.form = response.data
          this.formatterSecondaryBondCategory()
          this.loading = false
        })
      } else {
        getByWfNodeId(data.wfNodeId).then(response => {
          this.form = response.data
          this.formatterSecondaryBondCategory()
          if (data !== null) {
            this.form.fileList = data.fileList
            this.form.actTitle = data.actTitle
          }
          this.loading = false
        })
      }
    },
    formatterSecondaryBondCategory() {
      try {
        var temp = this.form.dealWfStorageBond.secondaryBondCategory
      } catch (e) {
        console.log(e)
      }
      this.$nextTick(() => {
        this.form.dealWfStorageBond.secondaryBondCategory = temp
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        dealId: undefined,
        dealName: undefined,
        actTitle: undefined,
        companyId: undefined,
        personCharge: undefined,
        dealType: undefined,
        industryFocus: undefined,
        dealSource: undefined,
        dateInvestmentEstimate: undefined,
        amountInvestmentEstimate: undefined,
        dealIntroduction: undefined,
        dealStrongPoint: undefined,
        dealScruple: undefined,
        dealWfStorageBond: {
          primaryBondCategory: '',
          secondaryBondCategory: '',
          bondCode: undefined,
          bondName: undefined,
          financingSubject: undefined,
          obligor: undefined,
          countryRisk: undefined,
          subjectName: undefined,
          series: undefined,
          nameCreditorRight: undefined,
          fundManager: undefined,
          fundSize: undefined,
          termInvestment: undefined,
          industryLevelOne: undefined,
          industryLevelTwo: undefined,
          currency: undefined,
          dueDate: undefined,
          repaymentOrder: undefined,
          level: undefined,
          structuredType: undefined,
          bondType: undefined,
          natureEnterprise: undefined,
          externalRatingIssuer: undefined,
          nameGuarantor: undefined,
          ratingOutsideGuarantor: undefined,
          thirdPartyPrincipal: undefined,
          dealBaseBondLevelList: []
        },
        fileList: []
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    /** 提交按钮 */
    submitForm: function() {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate(valid => {
          if (valid) {
            this.loading = true
            saveDealWfStorage(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess(this.$t('保存成功'))
                this.loading = false
                resolve(response.data.id)
              } else {
                this.msgError(response.msg)
                this.loading = false
                reject(response)
              }
            }).catch(() => {
              this.loading = false
            })
          } else {
            this.locationError(document)
            reject()
          }
        })
      })
    },
    editForm() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.dealBaseForm.openLocation = 'bfHome'
        this.$refs.dealBaseForm.init(this.$route.query)
      })
    }
  }

}
</script>
